package com.inossem.warehousestorage_inossem.adapter.inStorage;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageReceiveListActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.InStorageReceiveOverviewActivity;
import com.inossem.warehousestorage_inossem.activity.in_storage.extra.InStorageExtra;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageReceiveListBean;

import java.util.List;

import static com.inossem.warehousestorage_inossem.R.id.receiveNumber;

/**
 * Created by leij on 2017/6/6.
 */

public class ReceiveInStorageListAdapter extends BaseAdapter {

    private Context mContext;
    private List<InStorageReceiveListBean.DataBean.ReceiveInStorageListBean> mReceiveInStorageList;
    private LayoutInflater inflater;
    private InStorageReceiveListActivity.InStorageReceiveListener mInStorageReceiveListener;
    private String mInStorageNumber;

    public ReceiveInStorageListAdapter(Context context, List<InStorageReceiveListBean.DataBean.ReceiveInStorageListBean> receiveInStorageList, String inStorageNumber, InStorageReceiveListActivity.InStorageReceiveListener inStorageReceiveListener) {
        mContext = context;
        mReceiveInStorageList = receiveInStorageList;
        mInStorageReceiveListener = inStorageReceiveListener;
        inflater = LayoutInflater.from(context);
        mInStorageNumber = inStorageNumber;
    }

    @Override
    public int getCount() {
        return mReceiveInStorageList.size() + 1;
    }

    @Override
    public Object getItem(int position) {
        return mReceiveInStorageList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (position == 0) {
            convertView = inflater.inflate(R.layout.view_in_storage_receive_list_first_item, null);
            EditText inStorageNumber = (EditText) convertView.findViewById(R.id.inStorageNumber);
            ImageView search = (ImageView) convertView.findViewById(R.id.search);
            inStorageNumber.setText(mInStorageNumber == null ? "" : mInStorageNumber);
            inStorageNumber.setHint(mContext.getString(R.string.adapter_receive_in_storage_fill_in_number));
//            inStorageNumber.addTextChangedListener(new QRCodeEditTextWatcher(inStorageNumber) {
//                @Override
//                public void onScan(String text) {
//                    Utils.closeSoftKeyboard(mContext, inStorageNumber);
//                    mInStorageReceiveListener.onNoDoubleClickListener(text);
//                }
//            });
            search.setOnClickListener(new NoDoubleClickListener() {
                @Override
                public void onNoDoubleClick(View v) {
                    Utils.closeSoftKeyboard(mContext, v);
                    mInStorageReceiveListener.onNoDoubleClickListener(inStorageNumber.getText().toString());
                }
            });
        } else {
            InStorageReceiveListBean.DataBean.ReceiveInStorageListBean receiveInstorageItem = mReceiveInStorageList.get(position - 1);
            ViewHolder viewHolder;
            if (convertView == null || (convertView != null && convertView.getTag() == null)) {
                convertView = inflater.inflate(R.layout.view_in_storage_receive_list, null);
                viewHolder = new ViewHolder();
                //初始化控件
                initView(viewHolder, convertView);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            //初始化数据
            initData(mContext, viewHolder, convertView, receiveInstorageItem);
        }
        return convertView;
    }

    private void initView(ViewHolder viewHolder, View view) {
        viewHolder.receiveNumber = (TextView) view.findViewById(receiveNumber);
        viewHolder.purchaseOrder = (TextView) view.findViewById(R.id.purchaseOrder);
        viewHolder.supplier = (TextView) view.findViewById(R.id.supplier);
        viewHolder.inStorageNumber = (TextView) view.findViewById(R.id.inStorageNumber);
    }

    private void initData(Context context, ViewHolder viewHolder, View view, InStorageReceiveListBean.DataBean.ReceiveInStorageListBean receiveInStorageItem) {
        viewHolder.receiveNumber.setText(receiveInStorageItem.getReceiveNumber());
        viewHolder.purchaseOrder.setText(receiveInStorageItem.getPurchase());
        viewHolder.supplier.setText(receiveInStorageItem.getSupplier());
        viewHolder.inStorageNumber.setText(receiveInStorageItem.getInStorageNumber());
        view.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                Intent intent = new Intent(context, InStorageReceiveOverviewActivity.class);
                intent.putExtra(InStorageExtra.IN_STORAGE_NUMBER, viewHolder.inStorageNumber.getText().toString());
                context.startActivity(intent);
            }
        });
    }

    private class ViewHolder {
        private TextView receiveNumber, purchaseOrder, supplier, inStorageNumber;
    }
}
